import bus from "./bus"

/* 读取文件 */
export const readFile = (file) => {
	return new Promise(resolve => {
		let reader = new FileReader()
		reader.readAsBinaryString(file)
		reader.onload = ev => {
			resolve(ev.target.result)
		}
	})
}

export const dataURLtoFile = (dataurl,filename)=> {
	if(!dataurl){
		bus.$message.error("图片为空")
	}
	var arr = dataurl.split(",")
	var mime = arr[0].match(/:(.*?);/)[1]
	var bstr = atob(arr[1])
	var n = bstr.length
	var u8arr = new Uint8Array(n)
	while(n--){
		u8arr[n] = bstr.charCodeAt(n)
	}
	//转换成file对象
	return new File([u8arr], filename, {type:mime})
	//转换成成blob对象
	//return new Blob([u8arr],{type:mime});
}